﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class BrandBLL
  {
    public List<Brand> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Brands.OrderBy(p => p.Name).ToList();
      }
    }
    public List<Brand> GetByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Brands.Where(p => p.Mode == mode).OrderBy(p => p.Name).ToList();
      }
    }
    public List<Brand> GetByCateAndCateChild(int cateId, int cateChildId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Brands.Where(p => p.Mode == cateId || p.CateChildId == cateChildId).OrderBy(p => p.Name).ToList();
        }
    }


    public List<Brand> GetByCateParentId(int cateId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            List<ProdCate> list = data.ProdCates.Where(p => p.Parent == cateId).ToList();
            List<Brand> listBrand = new List<Brand>();
            foreach (var item in list)
            {
                List<Brand> products = GetByMode(item.Id);
                listBrand.AddRange(products);
            }
            return listBrand.OrderBy(p => p.Name).ToList();
        }
    }

    //public List<Brand> GetByProductCateId(int cateId)
    //{
    //    using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
    //    {
    //        List<Product> list = data.Products.Where(p => p.CategoryId == cateId).ToList();
    //        List<Brand> listBrand = new List<Brand>();
    //        foreach (var item in list)
    //        {
    //            List<Brand> products = GetByMode(item.Brand);
    //            listBrand.AddRange(products);
    //        }
    //        return listBrand.OrderBy(p => p.Name).ToList();
    //    }
    //}

    public List<Brand> GetByCateId(int cateId)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Brands.Where(p => p.CateId == cateId).OrderBy(p => p.Name).ToList();
        }
    }
    public Brand GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Brands.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    //Get max Sort

    public byte GetMaxSort(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Brand obj = data.Brands.Where(p => p.Mode == mode).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Brand objFirst = data.Brands.Where(p => p.Id == idFirst).FirstOrDefault();
        Brand objSecond = data.Brands.Where(p => p.Id == idSecond).FirstOrDefault();

        int tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }
    public void Add(Brand obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Brands.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }
    public void Update(Brand Brand)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Brand obj = data.Brands.Where(p => p.Id == Brand.Id).FirstOrDefault();
        if (obj != null)
        {
          obj.Name = Brand.Name;
          obj.Mode = Brand.Mode;
          obj.Sort = Brand.Sort;
          obj.CateId = Brand.CateId;
          obj.CateChildId = Brand.CateChildId;
        }
        data.SubmitChanges();
      }
    }
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Brand obj = data.Brands.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Brands.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
    public void DeleteByMode(int mode)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Brand> obj = data.Brands.Where(p => p.Mode == mode).ToList();
        if (obj != null)
        {
          data.Brands.DeleteAllOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
